#pragma once

#include <cstddef>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>

#include "file_system/file_reader/file_reader.h"

namespace hawking {
namespace indexlib {

// read only
class MmapFileReader : public FileReader {
public:
    // could_upadte means could modify in-place, but couldn't modify file-length
    MmapFileReader(const std::string& filepath, bool could_upadte = false) :
        FileReader(filepath) {
        could_upadte_ = could_upadte
    }
    virtual ~MmapFileReader();

    Status Init() override;
    Status Close() override;

    ReturnValue<size_t> Read(
        uint8_t* buffer, size_t length, size_t offset) override;

    ReturnValue<size_t> BatchRead(
        const std::vector<FilePart>& batch) override;

    FSFileType FileType() const override {
        return FSFileType::FSFT_MMAP;
    }

    size_t Length() const override {
        return file_length_;
    }

    ReturnValue<size_t> Write(
        const uint8_t* buffer, size_t length, size_t offset) override;

private:
    uint8_t* base_addr_ = nullptr;
    int32_t fd_ = -1;
    size_t file_length_ = 0;
    bool could_upadte_ = false;
};

}
}